define('utils/Time', null, function(){

let TimeHelper = function() {};

/**
 * 字符串左侧填充
 *
 * @param {String} str 待处理字符串
 * @param {String} pad 填充字符
 * @param {Number} length 处理后字符串长度
 * @return {String} 处理后的字符串
 */
TimeHelper.stringLPad = function (str, pad, length) {
    while (str.length < length) {
        str = pad + str;
    }
    return str;
};

/**
 * 字符串右侧填充
 *
 * @param {String} str 待处理字符串
 * @param {String} pad 填充字符
 * @param {Number} length 处理后字符串长度
 * @return {String} 处理后的字符串
 */
TimeHelper.stringRPad = function (str, pad, length) {
    while (str.length < length) {
        str = str + pad;
    }
    return str;
};

/**
 * 格式化时间
 *
 * @param {String} formats 格式化参数
 * @param {Number} timestamp 时间戳
 *
 * 用法
 * let str = TimeHelper.format('y-m-d h:i:s');
 *
 * @return {String}
 */
TimeHelper.format = function (formats, timestamp) {
    if (timestamp === void 0) { timestamp = Date.now(); }
    let d = new Date(timestamp);
    let funs = {
        y: function () { return d.getFullYear(); },
        m: function () { return TimeHelper.stringLPad(String(d.getMonth() + 1), '0', 2); },
        d: function () { return TimeHelper.stringLPad(String(d.getDate()), '0', 2); },
        h: function () { return TimeHelper.stringLPad(String(d.getHours()), '0', 2); },
        i: function () { return TimeHelper.stringLPad(String(d.getMinutes()), '0', 2); },
        s: function () { return TimeHelper.stringLPad(String(d.getSeconds()), '0', 2); }
    };
    return formats.replace(/(.?)/ig, function (match, p /* , offset, string */) {
        return undefined !== funs[match] ?
            funs[match]() :
            p;
    });
};
TimeHelper.friendlyTime = function(timestamp) {
    let now = new Date().getTime();

    let sub = now - timestamp;

    // 1 分钟内容
    if(sub < 60000) {
        return Math.floor(sub / 1000) + '秒前';
    }

    // 1 小时内容
    if(sub < 3600000) {
        return Math.floor(sub / 60000) + '分钟前';
    }

    // 1 天内
    if(sub < 86400000) {
        return Math.floor(sub / 3600000) + '小时前';
    }

    // 7 天内
    if(sub < 604800000) {
        return Math.floor(sub / 86400000) + '天前';
    }

    return TimeHelper.format('y-m-d h:i:s', timestamp);
};

return TimeHelper;
});
